// @ts-ignore
import { createRouter, createWebHistory } from 'vue-router'
import UserEditPage from '../views/back/user/editPage.vue'
import menuEditPage from '../views/back/Menu/editPage.vue'
import NewsEditPage from '../views/back/news/editPage.vue'


// @ts-ignore
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // {
    //   path: '/home',
    //   name: 'home',
    //   component: () => import('../views/back/index.vue'),
    // },
    {
      path: '/back',
      name: 'back',
      meta: { title: '管理系统' },
      component: () => import('../views/back/index.vue'),
      redirect:'HomePage',
      children:[
        {
        path: '/user',
        name: 'user',
        meta: { title: '用户管理' },
        component: () => import('../views/back/user/index.vue'),
        },
        // {
        //   path: '/user',
        //   name: 'user',
        //   component: () => import('../views/back/user/index.vue')
        // },
        {
          path: '/user/create',
          name: 'UserInsertData',
          meta: { title:'用户管理/新增学生' } ,
          component: () => import('../views/back/user/editPage.vue')
        },
        {
          path: '/user/:id/edit',
          name: 'UserEditData',
          component: () => import('../views/back/user/editPage.vue'),
          meta: { title:'用户管理/用户信息修改' }
        },
        {
          path: '/news/create',
          name: 'NewsCreateData',
          component: NewsEditPage,
          meta: { title:'系统公告/发布公告' }
        },
        {
          path: '/news/:id/edit',
          name: 'NewsEditData',
          component: NewsEditPage,
          meta: { title: '系统公告/修改公告' }
        },
        {
          path: '/news/:id/checkinfo',
          name: 'NewsInfoData',
          component: () => import('../views/back/news/newsinfo.vue'),
          meta: { title: '系统公告/公告详情' }
        },
        {
        path: '/base',
        name: 'base',
        meta: { title:'基础管理' },
        component: () => import('../views/home/video.vue'),
        },
        {
          path: '/MenuPage',
          name: 'MenuPage',
          meta: { title:'菜单管理' },
          component: () => import('@/views/back/NewMenu/index.vue'),
        }
        ,{
          path: '/menu/create',
          name: 'menuCreateData',
          component: menuEditPage,
          meta: { title:'菜单管理/菜单新增' },
        },
        {
          path: '/menu/:id/edit',
          name: 'menuEditData',
          component: menuEditPage,
          meta: { title:'菜单管理/菜单信息修改' },
        },
        {
          path: '/RolePage',
          name: 'Role',
          meta: { title:'角色管理' },
          component: () => import('../views/back/Role/index.vue'),
        },
        {
          path: '/role/edit',
          name: 'RolCreateData',
          meta: { title:'角色管理/角色新增' },
          component: () => import('../views/back/Role/editPage.vue'),
        },
        {
          path: '/role/:id/edit',
          name: 'Roledit',
          meta: { title: '角色管理/角色信息修改' },
          component: () => import('../views/back/Role/editPage.vue'),
        },
        {
          path: '/TeacherPage',
          name: 'TeacherPage',
          meta: { title:'用户管理' },
          component: () => import('../views/back/teacher/index.vue'),
        },
        {
          path: '/teacher/:id/edit',
          name: 'TeacherPageEditData',
          component: () => import('../views/back/teacher/changePermissions.vue'),
          meta: { title: '用户管理/更改权限' }
        },
        {
          path: '/teacher/create',
          name: 'TeacherPageCreateData',
          component: () => import('../views/back/teacher/editPage.vue'),
          meta: { title:'用户管理/添加管理员' }
        },
        {
          path: '/LoginPage',
          name: 'LoginPage',
          meta: { title:'日志管理' },
          component: () => import('../views/back/log/log .vue'),
        },
        {
          path: '/CoursePage',
          name: 'CoursePage',
          meta: { title: '课程管理' },
          component: () => import('../views/back/course/index.vue'),
        },
        {
          path: '/NewsPage',
          name: 'NewsPage',
          meta: { title: '系统公告' },
          component: () => import('../views/back/news/index.vue'),
        },
        {
          path: '/OrderPage',
          name: 'OrderPage',
          meta: { title: '订单管理' },
          component: () => import('../views/back/order/index.vue'),
        },
        {
          path: '/PersonalCenter',
          name: 'PersonalCenter',
          meta: { title: '个人中心' },
          component: () => import('../views/back/personalcenter/index.vue'),
        },
        {
          path: '/HomePage',
          name: 'HomePage',
          meta: { title: '后台首页' },
          component: () => import('../views/back/backhome/index.vue'),
        },
        {
          path: '/YearPage',
          name: 'YearPage',
          meta: { title: '年销售管理' },
          component: () => import('@/views/back/year/index.vue'),
        },
      ]
    },
    {
      path: '/',
      name: 'login',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/login/index.vue'),
    },
    {
      path: '/register',
      name: 'register',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/register/index.vue'),
    },
  ],
})
// 放行白名单（前台页面，不需要登录的页面）
// 白名单数组：手动添加，要么后端返回
const whiteList = ['/', '/register'];

// 全局前置守卫, 路由守卫（导航守卫），路由跳过去之前拦截
//to : 到哪里去， from 从哪里来，next: 放行
router.beforeEach((to, from, next) => {
  // 如果访问的是登录页面，直接放行
  if (whiteList.includes(to.path)) {
    next() // 放行到登录页面
  }
  else {
    // 如果访问的是其他页面，判断是否有token
    const token = localStorage.getItem('token')
    if (token) {
      // 有token，放行
      next()
    } else {
      // 没有token，跳转到登录页面
      next('/')
    }
  }
})
// router.beforeEach((to,from, next) => {
//   // console.log(to.path,router);
//   sessionStorage.setItem("ActiveIndex",to.path);
//   // store.commit("setActiveIndex",to.path)
//   next();
// });

//判断用户是否登录
function checkIsUseLog(){
  const token = sessionStorage.getItem("token");
  if(!token){
    return false;
  }
  return true;
}
// // //注册请求拦截器
// axios.interceptors.request.use(request =>{
//       //1.获取sessionStorage中保存的token
//       const token = sessionStorage.getItem("token");
//       // 2.判断token是否为null
//       if(token!=null&&token!=''&&token!='undefined'){ //表示token是否有值
//         // 3.把token跟随当前请求携带到后端
//         request.headers.authorization = token;
//       }
//       return request;
//     },
//     error => {
//       //对响应错误做点什么
//       return Promise.reject(error);
//     });
//
// //注册响应拦截器
// ///
// axios.interceptors.response.use( resp => {
//       // console.log("resp =>" + resp.headers.authorization);
//       // //1.从响应里获取token
//       // const token =resp.headers.authorization;
//       // sessionStorage.setItem('token',token);
//       // //判断响应码
//       // if(resp.data.code == 500){
//       //     // ElMessage({
//       //     //     showClose: true,
//       //     //     message: resp.data.msg,
//       //     //     type: 'error',
//       //     // });
//       //     router.push('/');
//       // }
//       return resp;
//     },
//     error => {
//       //对响应错误做点什么
//       return Promise.reject(error);
//     });
export default router